package com.scs.application.modules.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.modules.base.dto.UserWareHouseDTO;
import com.scs.application.modules.base.entity.UserWarehouse;
import com.scs.application.modules.base.service.UserWarehouseService;
import com.scs.application.modules.base.vo.UserWarehouseVO;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/base/user_warehouse")
public class UserWarehouseController extends CrudController<UserWarehouseService, UserWarehouse>{
    @Autowired
    UserWarehouseService service;
    @GetMapping("/getByCurrentUser")
    public RestResult getByCurrentUser() {
       List<UserWarehouseVO> result = service.getUserWarehouse();
        return RestResult.ok("查询成功", result);
    }

    @GetMapping("/getBySettype")
    public RestResult getBySettype(@RequestParam String id,@RequestParam String setTypeField) {
        List<UserWarehouse> result = service.list(new QueryWrapper<UserWarehouse>().eq(setTypeField,id));
        return RestResult.ok("查询成功", result);
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("set")
    public RestResult setUserWareHouse(@RequestBody @Valid UserWareHouseDTO userWareHouseDTO) {
        service.setUserWareHouse(userWareHouseDTO);
        return RestResult.ok();
    }

    @GetMapping("/getUserWarehouseBySettype")
    public RestResult getUserWarehouseBySettype(@RequestParam String id,@RequestParam String setTypeField) {
        List<UserWarehouseVO> result = service.getUserWarehouseBySettype(setTypeField+" = '"+id+"'");
        return RestResult.ok("查询成功", result);
    }


    @GetMapping("/setdeptWarehouseDefault")
    public RestResult setdeptWarehouseDefault(@RequestParam String deptId,@RequestParam String warehouseId) {
        service.update(Wrappers.<UserWarehouse>update().set("flag_default",0).eq("dept_id",deptId));
        service.update(Wrappers.<UserWarehouse>update().set("flag_default",1).eq("dept_id",deptId).eq("warehouse_id",warehouseId));
        return RestResult.ok("操作成功");
    }
}
